# coding=utf-8
import sqlite3,subprocess,requests,json

p = subprocess.Popen(['adb', 'shell', 'pm', 'list', 'packages', '-3'],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output, err = p.communicate()

# 输出短信内容
if output:
	# print("短信内容:")
	# print(output.decode('utf-8'))
	apps_data=output.decode('utf-8')
	# print(sms_data)
else:
	pass

site=open("config.json","r")
site=site.read()
site=json.loads(site)
data={"s":apps_data}
url=f"{site['site']}/application.php"
r=requests.post(url=url,data=data)
rs=json.loads(r.text)


connect=sqlite3.connect('data/sms.db')


cursor=connect.cursor()

sql=f"delete from application";
cursor.execute(sql)

for x in range(0,len(rs)):
	sql=f"insert into application (packagename)values('{rs[x]}')"
	cursor.execute(sql)
	connect.commit()

# print("执行成功")


cursor.close()

connect.close()


# print({"msg":"successfull"})